//
//  HZCategoryGoodsListHeadView.m
//  app
//
//  Created by 刘华舟 on 15/5/25.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZCategoryGoodsListHeadView.h"

#import "HZImageSourceConstant.h"

#import "UIImageView+URL.h"
#import "UIButton+URL.h"

static const NSInteger kMainViewMargin = 24;
static const NSInteger kMainViewPadding = 10;
static const NSInteger kLabelViewHeight = 60;
static NSString * const kMallSearchText = @" 搜索商品";

@interface HZCategoryGoodsListHeadView()

    
@property (strong, nonatomic) UILabel *searchLabel;
@property (strong, nonatomic) UIButton *voiceBtn;


@property (strong, nonatomic) NSArray* topData;
@property (strong, nonatomic) NSArray* bottomData;

@property (strong, nonatomic) NSMutableArray *topBtnArray;
@property (strong, nonatomic) NSMutableArray *bottomBtnArray;

@end

@implementation HZCategoryGoodsListHeadView

-(NSMutableArray *)topBtnArray{
    if (_topBtnArray == nil) {
        _topBtnArray = [NSMutableArray array];
    }
    return _topBtnArray;
}

-(NSMutableArray *)bottomBtnArray{
    if (_bottomBtnArray == nil) {
        _bottomBtnArray = [NSMutableArray array];
    }
    return _bottomBtnArray;
}

+ (instancetype)cellWithCollectionView:(HZCollectionView *)collectionView dequeueReusableSupplementaryViewOfKind:(NSString*)elementKind forIndexPath:(NSIndexPath *)indexPath{
    NSString *cellReuseIdentifier = @"HZCategoryGoodsListHeadView";
    HZCategoryGoodsListHeadView * header= (HZCategoryGoodsListHeadView *)[collectionView dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:cellReuseIdentifier forIndexPath:indexPath];
    if (header == nil) {
        NSLog(@"创建HZGoodsCategoryColHeadView失败");
    }
    return header;
}

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        self.frame = CGRectOf(self.frame.origin, [UBO_SCREEN fw:UB_WIDTH uh:kMainViewHeight]);
        //label
        _searchLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.ufo fx:UB_LEFT ux:kMainViewMargin fy:UB_BOTTOM uy:kMainViewPadding],
                                                                   [self.ubo fw:UB_WIDTH uw:-2*kMainViewMargin uh:kLabelViewHeight])];
        _searchLabel.textAlignment = NSTextAlignmentCenter;
        _searchLabel.backgroundColor = [HZ_WHITE_COLOR colorWithAlphaComponent:1.0f];
        
        _searchLabel.layer.masksToBounds = YES;
        _searchLabel.layer.cornerRadius = [self.ubo uw:4];
        _searchLabel.layer.borderColor = HZ_MALL_BORDER_LINE_GRAY_COLOR.CGColor;
        _searchLabel.layer.borderWidth = [self.ubo pw:1];
        
        NSTextAttachment * textAttachment = [[NSTextAttachment alloc] init];//添加附件,图片
        
        UIImage *image = [UIImage imageNamed:kImageMallHomeSearch];
        
        textAttachment.image = image;
        
        textAttachment.bounds = CGRectMake(0, -2.5, image.size.height , image.size.width);
        
        NSAttributedString *imgString = [NSMutableAttributedString attributedStringWithAttachment:textAttachment];
        NSMutableAttributedString *atrString = [[NSMutableAttributedString alloc] initWithString:kMallSearchText];
        [atrString addAttribute:NSForegroundColorAttributeName value:HZ_MALL_TEXT_M_LIGHTGRAY_COLOR range:NSMakeRange(0, kMallSearchText.length)];
        [atrString addAttribute:NSFontAttributeName value:kHZFont_Content_M range:NSMakeRange(0, kMallSearchText.length)];
        [atrString insertAttributedString:imgString atIndex:0];
        
        _searchLabel.attributedText = atrString;
        
        UITapGestureRecognizer *tapGestr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(searchViewOnClick)];
        [_searchLabel addGestureRecognizer:tapGestr];
        _searchLabel.userInteractionEnabled = YES;
        [self addSubview:_searchLabel];
        
        //voiceBtn
        _voiceBtn = [[UIButton alloc] initWithFrame:CGRectOf([_searchLabel.ufo fx:UB_RIGHT ux:-kLabelViewHeight fy:UB_TOP],
                                                                 [_searchLabel.ubo uw:kLabelViewHeight fh:UB_HEIGHT])];
        [_voiceBtn setImage:[UIImage imageNamed:kImageMallHomeVoice] forState:UIControlStateNormal];
        [_voiceBtn addTarget:self action:@selector(voiceButtonOnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_voiceBtn];
        
    }
    return self;
}




-(void)didTopImageBtn:(UIButton*)sender
{

    if (self.pageTappedCaller) {
        [self.pageTappedCaller call:self.topData];
    }

}

-(void)didBottomImageBtn:(UIButton*)sender
{

    if (self.pageTappedCaller) {
        [self.pageTappedCaller call:self.bottomData];
    }
}

-(void)showTopPages:(NSArray*)topPages bottomPages:(NSArray *)bottomPages imageKey:(NSString*)imageKey
{
    
    for (UIButton *btn in self.topBtnArray) {
        [btn removeFromSuperview];
    }
    [self.topBtnArray removeAllObjects];
    
    self.topData = topPages;
    if (topPages.count == 1) {
        UIButton *topABtn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP uy:kMainViewMargin],
                                                                     [self.ubo fw:UB_WIDTH oh:[self.ubo fw:UB_1_3]])];
        [topABtn setImageForState:UIControlStateNormal withString:[[topPages objectAtIndex:0] stringForKey:imageKey]];
        [topABtn setBackgroundColor:HZ_MALL_S_LIGHT_GRAY_BACKGROUND_COLOR];
        topABtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
        topABtn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
        [topABtn addTarget:self action:@selector(didTopImageBtn:) forControlEvents:UIControlEventTouchUpInside];
        topABtn.tag = 0;
        [self addSubview:topABtn];
        [self.topBtnArray addObject:topABtn];
    }else if(topPages.count == 2){
        UIButton *topABtn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP uy:kMainViewMargin],
                                                                     [self.ubo fw:UB_1_2 pw:-(1) oh:[self.ubo fw:UB_1_3]])];
        [topABtn setImageForState:UIControlStateNormal withString:[[topPages objectAtIndex:0] stringForKey:imageKey]];
        [topABtn setBackgroundColor:HZ_MALL_S_LIGHT_GRAY_BACKGROUND_COLOR];
        topABtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
        topABtn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
        [topABtn addTarget:self action:@selector(didTopImageBtn:) forControlEvents:UIControlEventTouchUpInside];
        topABtn.tag = 0;
        [self addSubview:topABtn];
        [self.topBtnArray addObject:topABtn];
        
        UIButton *topBBtn = [[UIButton alloc] initWithFrame:CGRectOf([topABtn.ufo fx:UB_RIGHT px:(1) fy:UB_TOP],
                                                                     [topABtn.ubo fw:UB_WIDTH pw:-(1) fh:UB_HEIGHT])];
        [topBBtn setImageForState:UIControlStateNormal withString:[[topPages objectAtIndex:0] stringForKey:imageKey]];
        [topBBtn setBackgroundColor:HZ_MALL_S_LIGHT_GRAY_BACKGROUND_COLOR];
        topBBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
        topBBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
        [topBBtn addTarget:self action:@selector(didTopImageBtn:) forControlEvents:UIControlEventTouchUpInside];
        topBBtn.tag = 1;
        [self addSubview:topBBtn];
        [self.topBtnArray addObject:topBBtn];
    }else if(topPages.count == 3){
        UIButton *topABtn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP uy:kMainViewMargin],
                                                                     [self.ubo fw:UB_1_2 pw:-(1) oh:[self.ubo fw:UB_1_2]])];
        [topABtn setImageForState:UIControlStateNormal withString:[[topPages objectAtIndex:0] stringForKey:imageKey]];
        [topABtn setBackgroundColor:HZ_MALL_S_LIGHT_GRAY_BACKGROUND_COLOR];
        topABtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
        topABtn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
        [topABtn addTarget:self action:@selector(didTopImageBtn:) forControlEvents:UIControlEventTouchUpInside];
        topABtn.tag = 0;
        [self addSubview:topABtn];
        [self.topBtnArray addObject:topABtn];
        
        UIButton *topBBtn = [[UIButton alloc] initWithFrame:CGRectOf([topABtn.ufo fx:UB_RIGHT px:(1) fy:UB_TOP],
                                                                     [topABtn.ubo fw:UB_WIDTH pw:-(1) fh:UB_1_2 ph:-(1)])];
        [topBBtn setImageForState:UIControlStateNormal withString:[[topPages objectAtIndex:0] stringForKey:imageKey]];
        [topBBtn setBackgroundColor:HZ_MALL_S_LIGHT_GRAY_BACKGROUND_COLOR];
        topBBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
        topBBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
        [topBBtn addTarget:self action:@selector(didTopImageBtn:) forControlEvents:UIControlEventTouchUpInside];
        topBBtn.tag = 1;
        [self addSubview:topBBtn];
        [self.topBtnArray addObject:topBBtn];
        
        UIButton *topCBtn = [[UIButton alloc] initWithFrame:CGRectOf([topABtn.ufo fx:UB_RIGHT px:(1) fy:UB_CENTER py:2],
                                                                     [topBBtn.ubo fw:UB_WIDTH fh:UB_HEIGHT])];
        [topCBtn setImageForState:UIControlStateNormal withString:[[topPages objectAtIndex:0] stringForKey:imageKey]];
        [topCBtn setBackgroundColor:HZ_MALL_S_LIGHT_GRAY_BACKGROUND_COLOR];
        topCBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
        topCBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
        [topCBtn addTarget:self action:@selector(didTopImageBtn:) forControlEvents:UIControlEventTouchUpInside];
        topCBtn.tag = 2;
        [self addSubview:topCBtn];
        [self.topBtnArray addObject:topCBtn];
    }
    
    for (UIButton *btn in self.bottomBtnArray) {
        [btn removeFromSuperview];
    }
    [self.bottomBtnArray removeAllObjects];
    
    self.bottomData = bottomPages;
    
    if (bottomPages.count == 1) {
        UIButton *topButton = [self.topBtnArray objectAtIndex:0];
        UIButton *bottomABtn = [[UIButton alloc] initWithFrame:CGRectOf([topButton.ufo fx:UB_LEFT fy:UB_BOTTOM uy:kMainViewMargin],
                                                                        [self.ubo fw:UB_WIDTH oh:[self.ubo fw:UB_1_3]])];
        [bottomABtn setImageForState:UIControlStateNormal withString:[[topPages objectAtIndex:0] stringForKey:imageKey]];
        [bottomABtn setBackgroundColor:HZ_MALL_S_LIGHT_GRAY_BACKGROUND_COLOR];
        bottomABtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
        [bottomABtn addTarget:self action:@selector(didBottomImageBtn:) forControlEvents:UIControlEventTouchUpInside];
        bottomABtn.tag = 0;
        [self addSubview:bottomABtn];
        [self.bottomBtnArray addObject:bottomABtn];
    }else if (bottomPages.count >= 3){
        UIButton *topButton = [self.topBtnArray objectAtIndex:0];
        UIButton *bottomABtn = [[UIButton alloc] initWithFrame:CGRectOf([topButton.ufo fx:UB_LEFT fy:UB_BOTTOM uy:kMainViewMargin],
                                                                        [self.ubo fw:UB_1_3 ow:-(1) oh:[self.ubo fw:UB_1_3]])];
        [bottomABtn setImageForState:UIControlStateNormal withString:[[topPages objectAtIndex:0] stringForKey:imageKey]];
        [bottomABtn setBackgroundColor:HZ_MALL_S_LIGHT_GRAY_BACKGROUND_COLOR];
        bottomABtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
        [bottomABtn addTarget:self action:@selector(didBottomImageBtn:) forControlEvents:UIControlEventTouchUpInside];
        bottomABtn.tag = 0;
        [self addSubview:bottomABtn];
        [self.bottomBtnArray addObject:bottomABtn];
        
        UIButton *bottomBBtn = [[UIButton alloc] initWithFrame:CGRectOf([bottomABtn.ufo fx:UB_RIGHT ox:(2) fy:UB_TOP], [bottomABtn.ubo fw:UB_WIDTH fh:UB_HEIGHT])];
        [bottomBBtn setImageForState:UIControlStateNormal withString:[[topPages objectAtIndex:0] stringForKey:imageKey]];
        [bottomBBtn setBackgroundColor:HZ_MALL_S_LIGHT_GRAY_BACKGROUND_COLOR];
        bottomBBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
        [bottomBBtn addTarget:self action:@selector(didBottomImageBtn:) forControlEvents:UIControlEventTouchUpInside];
        bottomBBtn.tag = 1;
        [self addSubview:bottomBBtn];
        [self.bottomBtnArray addObject:bottomBBtn];
        
        UIButton *bottomCBtn = [[UIButton alloc] initWithFrame:CGRectOf([bottomBBtn.ufo fx:UB_RIGHT ox:2 fy:UB_TOP], [bottomBBtn.ubo fw:UB_WIDTH fh:UB_HEIGHT])];
        [bottomCBtn setImageForState:UIControlStateNormal withString:[[topPages objectAtIndex:0] stringForKey:imageKey]];
        [bottomCBtn setBackgroundColor:HZ_MALL_S_LIGHT_GRAY_BACKGROUND_COLOR];
        bottomBBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
        [bottomCBtn addTarget:self action:@selector(didBottomImageBtn:) forControlEvents:UIControlEventTouchUpInside];
        bottomCBtn.tag = 2;
        [self addSubview:bottomCBtn];
        [self.bottomBtnArray addObject:bottomCBtn];
    }
    
    CGFloat topAndBottom = 0.0f;
    if (self.topBtnArray.count > 0) {
        UIButton *topLastBtn = [self.topBtnArray lastObject];
        topAndBottom = topLastBtn.ufo.yy;
        
        if (self.bottomBtnArray.count > 0) {
            UIButton *bottomLastBtn = [self.bottomBtnArray lastObject];
            topAndBottom = bottomLastBtn.ufo.yy;
        }
    }
    
    if (self.bottomBtnArray.count > 0 || self.topBtnArray.count > 0) {
        topAndBottom += [self.ubo uh:kMainViewPadding];
    }
    
    self.frame = CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP],[self.ubo fw:UB_WIDTH uh:kMainViewHeight oh:topAndBottom]);
    
    _searchLabel.frame = CGRectOf([self.ubo fx:UB_LEFT ux:kMainViewMargin fy:UB_BOTTOM uy:-(kLabelViewHeight + kMainViewPadding)],
                                  [self.ubo fw:UB_WIDTH uw:-2*kMainViewMargin uh:kLabelViewHeight]);
    _voiceBtn.frame = CGRectOf([_searchLabel.ufo fx:UB_RIGHT ux:-kLabelViewHeight fy:UB_TOP],
                               [_searchLabel.ubo uw:kLabelViewHeight fh:UB_HEIGHT]);
    
    
}



//点击其他搜索区域
- (void)searchViewOnClick{
    HZLog(@"搜索点击");
    if (self.sDelegate && [self.sDelegate respondsToSelector:@selector(mallFindSearchTap)]) {
        [self.sDelegate mallFindSearchTap];
    }
}

- (void)voiceButtonOnClick{
    HZLog(@"点击语音");
    if (self.sDelegate && [self.sDelegate respondsToSelector:@selector(mallFindVoiceSearchTap)]) {
        [self.sDelegate mallFindVoiceSearchTap];
    }
}


@end
